/*
* $Id: AddChangeFamilyView.cpp 162 2010-10-04 11:21:18Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddChangeFamilyView.h"
#include "Event/EventId.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/ComboBoxItem.h"
#include "Settings/Journal/FamilyViewSettings.h"
#include "Settings/SettingControlIds.h"
#include "Settings/DinIntDefined.h"

#pragma warning(disable: 4407)

#define TITLE_ID			1
#define NAME_FAMILY_ID		2
#define FAMILY_TYPE_ID		3
#define NOTE_FAMILY_ID		4
#define USER_FIELD_ID		5

/************************************************************************/
/*                     CAddChangeFamilyView                             */
/************************************************************************/
CAddChangeFamilyView::CAddChangeFamilyView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){
	IEventManager::GetEventManager()->SubscribeEvent(dynamic_cast<ISubscriber*>(this), FAMILY_STN_CHANGE_EVENT);
}
CAddChangeFamilyView::~CAddChangeFamilyView(){
	IEventManager::GetEventManager()->UnSubscribeEvent(dynamic_cast<ISubscriber*>(this), FAMILY_STN_CHANGE_EVENT);
}

void CAddChangeFamilyView::AddTypesFamily(CComboBoxItem* pItem){
	pItem->AddValueItem(Family::DEFAULT, ILanguageManager::GetLanguageManager()->GetString(TYPE_DEFAULT_ID));
	pItem->AddValueItem(Family::PEDIGREE, ILanguageManager::GetLanguageManager()->GetString(TYPE_PEDIGREE_ID));
	pItem->AddValueItem(Family::BREEDING, ILanguageManager::GetLanguageManager()->GetString(TYPE_BREEDING_ID));
	pItem->AddValueItem(Family::FATHER, ILanguageManager::GetLanguageManager()->GetString(TYPE_FATHER_ID));
	pItem->AddValueItem(Family::AMADOU, ILanguageManager::GetLanguageManager()->GetString(TYPE_AMADOU_ID));
	pItem->AddValueItem(Family::BARREN, ILanguageManager::GetLanguageManager()->GetString(TYPE_BARREN_ID));
	pItem->AddValueItem(Family::QUEEN_CELL, ILanguageManager::GetLanguageManager()->GetString(TYPE_QUEEN_CELL_ID));
	pItem->AddValueItem(Family::QUEEN_CELL_SMALL, ILanguageManager::GetLanguageManager()->GetString(TYPE_QUEEN_CELL_SMALL_ID));
	pItem->AddValueItem(Family::INDIVIDUAL_PROVINE, ILanguageManager::GetLanguageManager()->GetString(TYPE_INDIVIDUAL_PROVINE_ID));
	pItem->AddValueItem(Family::COMBINED_PROVINE, ILanguageManager::GetLanguageManager()->GetString(TYPE_COMBINED_PROVINE_ID));
	pItem->AddValueItem(Family::PACKET, ILanguageManager::GetLanguageManager()->GetString(TYPE_PACKET_ID));
	pItem->AddValueItem(Family::NON_QUEEN, ILanguageManager::GetLanguageManager()->GetString(TYPE_NON_QUEEN_ID));
	pItem->AddValueItem(Family::SWARM, ILanguageManager::GetLanguageManager()->GetString(TYPE_SWARM_ID));
	pItem->AddValueItem(Family::USER, ILanguageManager::GetLanguageManager()->GetString(TYPE_USER_ID));
}

void CAddChangeFamilyView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(GetHeaderString(), TITLE_ID));

	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::family fml = pManager->GetFamily(m_nIdParameter);
	m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_NAME_ID), NAME_FAMILY_ID, editItem::TEXT, m_aCurValues[NAME_FAMILY_ID]));
	CFamilyViewSettings familyStn;
	CComboBoxItem* pItem = new CComboBoxItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_TYPE_ID), FAMILY_TYPE_ID);
	AddTypesFamily(pItem);
	pItem->SetValue(_ttoi(m_aCurValues[FAMILY_TYPE_ID].c_str()));
	m_editorDrawer.AddItem(pItem);
	if(familyStn.GetCurrentValue(CHECK_TYPE_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	CEditItem *pEditItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_FAMILY_ID, editItem::TEXT, m_aCurValues[NOTE_FAMILY_ID]);
	m_editorDrawer.AddItem(pEditItem);
	if(familyStn.GetCurrentValue(CHECK_NOTE_ID) != CHECK_BOX_STATE_1)
		pEditItem->Hide(true);
	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(TypeField::FAMILIES, USER_FIELD_ID, m_nIdRecord, m_aCurValues);
}

void CAddChangeFamilyView::GetCurValue(){
	m_aCurValues[NAME_FAMILY_ID] = m_editorDrawer.GetValueItem(NAME_FAMILY_ID);
	m_aCurValues[NOTE_FAMILY_ID] = m_editorDrawer.GetValueItem(NOTE_FAMILY_ID);
	m_aCurValues[FAMILY_TYPE_ID] = m_editorDrawer.GetValueItem(FAMILY_TYPE_ID);
	for(int i = 1; i <= m_nUserFields; i++)
		m_aCurValues[USER_FIELD_ID + i] = m_editorDrawer.GetValueItem(USER_FIELD_ID + i);
}

void CAddChangeFamilyView::GetSaveData(CFamilyManager::family& fml, std::map<std::tstring, std::tstring>& aUserField){
	fml.m_strName = m_editorDrawer.GetValueItem(NAME_FAMILY_ID);
	fml.m_strNote = m_editorDrawer.GetValueItem(NOTE_FAMILY_ID);
	fml.m_type = (Family::type)_ttoi(m_editorDrawer.GetValueItem(FAMILY_TYPE_ID).c_str());
	for(int i = 1; i <= m_nUserFields; i++)
		aUserField.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
}

/************************************************************************/
/*                         CAddFamilyView                               */
/************************************************************************/
CAddFamilyView::CAddFamilyView(CState* pState)
: CAddChangeFamilyView(pState){
	m_nIdRecord = 0;
}
CAddFamilyView::~CAddFamilyView(){}
std::tstring CAddFamilyView::GetHeaderString(){
	return ILanguageManager::GetLanguageManager()->GetString(ADD_FAMILY_ID);
}
void CAddFamilyView::SetState(){
	m_pState->m_nState = Journal::VIEW_ADD_FAMILY;
}
bool CAddFamilyView::IsViewState(State state){
	return state.m_type == Journal::ADD_FAMILY;
}

BEGIN_MESSAGE_MAP(CChangeFamilyView, CAddChangeFamilyView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSaveFamily)
END_MESSAGE_MAP()

void CChangeFamilyView::OnSaveFamily(){
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::family fml;
	std::map<std::tstring, std::tstring> aUserFields;
	GetSaveData(fml, aUserFields);
	pManager->ChangeFamily(m_nIdParameter, fml, aUserFields);
	__super::OnCancel();
}

/************************************************************************/
/*                        CChangeFamilyView                             */
/************************************************************************/
CChangeFamilyView::CChangeFamilyView(CState* pState)
: CAddChangeFamilyView(pState){}
CChangeFamilyView::~CChangeFamilyView(){}
std::tstring CChangeFamilyView::GetHeaderString(){
	return ILanguageManager::GetLanguageManager()->GetString(CHANGE_FAMILY_ID);
}
void CChangeFamilyView::SetState(){
	m_pState->m_nState = Journal::VIEW_CHANGE_FAMILY;
}
bool CChangeFamilyView::IsViewState(State state){
	return state.m_type == Journal::CHANGE_FAMILY;
}
void CChangeFamilyView::InitCurrentValue(){
	CFamilyManager::family fml = IJournalComponent::GetFamilyManager()->GetFamily(m_nIdParameter);
	m_aCurValues[NAME_FAMILY_ID] = fml.m_strName;
	m_aCurValues[NOTE_FAMILY_ID] = fml.m_strNote;
	m_aCurValues[FAMILY_TYPE_ID] = CConvertFunction::ConvertFromInt(fml.m_type);
	m_nIdRecord = fml.m_nIdRecord;
}

BEGIN_MESSAGE_MAP(CAddFamilyView, CAddChangeFamilyView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSaveFamily)
END_MESSAGE_MAP()

void CAddFamilyView::OnSaveFamily(){
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::family fml;
	fml.m_nIdPoint = m_nIdParameter;
	std::map<std::tstring, std::tstring> aUserFields;
	GetSaveData(fml, aUserFields);
	int nId = pManager->AddFamily(fml, aUserFields);
	IWnd* pWnd = IVisualManager::GetVisualManager()->
		GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	pWnd->Post(UM_ITEM_MANIPULATION, Journal::ADD_INSPECT, nId);
}
